package com.example.a02back.controller;


import com.example.a02back.common.APIResponse;
import com.example.a02back.service.impl.AuthServicelmpl;
import com.example.a02back.service.impl.TradeServiceImpl;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SimpChaos
 * @since 2022-07-14
 */
@Controller
@ResponseBody
@RestController
@RequestMapping("/trade")
public class TradeController {
    @Resource
    private AuthServicelmpl auth;
    @Resource
    private TradeServiceImpl tradeService;


    /**
     * @Description:  敲定
     * @Author: SimpChaos
     * @Date: 2022/7/21 0:59
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @RequestMapping("/accept")
    @CrossOrigin
    public APIResponse<?> accepcTrade(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String nowUid = httpServletRequest.getParameter("uid");
        String swapper = httpServletRequest.getParameter("swapper");
        String sid = httpServletRequest.getParameter("sid");
        return APIResponse.success("SUCCESS",tradeService.accepcTrade(swapper,sid));
    }

    /**
     * @Description:  取消敲定
     * @Author: SimpChaos
     * @Date: 2022/7/21 0:59
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @RequestMapping("/cancel")
    @CrossOrigin
    public APIResponse<?> cancelTrade(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String sid = httpServletRequest.getParameter("sid");
        return APIResponse.success("SUCCESS",tradeService.cancelTrade(sid));
    }



    /**
     * @Description:  敲定
     * @Author: SimpChaos
     * @Date: 2022/7/21 0:59
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @RequestMapping("/finish")
    @CrossOrigin
    public APIResponse<?> finishTrade(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String sid = httpServletRequest.getParameter("sid");
        return APIResponse.success("SUCCESS",tradeService.finishTrade(sid));
    }



}
